#!/usr/bin/env bash
set -e
mkdir -p /opt/tmp/
mkdir -p /opt/script/




wget https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/stable/zookeeper-3.4.10.tar.gz
tar -zxvf zookeeper-3.4.10.tar.gz

mv zookeeper-3.4.10/ /opt/zookeeper
mv zookeeper-3.4.10.tar.gz /opt/tmp/


mkdir -p /opt/zookeeper/zookeeper/data
mkdir -p /opt/zookeeper/zookeeper/datalog
mkdir -p /opt/zookeeper/zookeeper/log

cp /opt/zookeeper/conf/zoo_sample.cfg /opt/zookeeper/conf/zoo.cfg


#scp hk:/opt/zookeeper/conf/zoo.cfg  /opt/zookeeper/conf/


num=`cat config.json  |jq '.server | length'`

for((i=0;i<$num;i++))
do
host=`cat config.json  |jq ".server[$i].host"`
ip=`cat config.json  |jq ".server[$i].ip"`
if  [ "\""`hostname`"\"" = ${host} ]
then
#         echo    "server.${i}=0.0.0.0:2888:3888" | sed 's/\"//g'  >>/opt/zookeeper/conf/zoo.cfg
         echo     ${i} >>/opt/zookeeper/zookeeper/data/myid
#else
#        echo    "server.${i}=${ip}:2888:3888" | sed 's/\"//g'  >>/opt/zookeeper/conf/zoo.cfg
fi
echo    "server.${i}=${host}:2888:3888" | sed 's/\"//g'  >>/opt/zookeeper/conf/zoo.cfg

done




sed -i 's/\(dataDir*=\).*/\1 \/opt\/zookeeper\/zookeeper\/data /g'  /opt/zookeeper/conf/zoo.cfg
echo "dataLogDir=/opt/zookeeper/zookeeper/datalog" >>/opt/zookeeper/conf/zoo.cfg

echo -e  "#!/bin/env bash
/opt/zookeeper/bin/zkServer.sh start /opt/zookeeper/conf/zoo.cfg
">>/opt/script/zkup.sh

echo -e  "#!/bin/env bash
/opt/zookeeper/bin/zkServer.sh stop
">>/opt/script/zkdown.sh

echo -e  "#!/bin/env bash
/opt/zookeeper/bin/zkServer.sh status
">>/opt/script/zkstatus.sh

echo -e  "#!/bin/env bash
/opt/zookeeper/bin/zkServer.sh stop
rm -rf /opt/zookeeper
rm -rf /opt/script/zkdown.sh
rm -rf opt/script/zkstatus.sh
rm -rf /opt/script/zkstatus.sh
rm -rf /opt/script/zkuninstall.sh
">>/opt/script/zkuninstall.sh



#/opt/zk/zookeeper-3.4.6/bin/zkServer.sh start
#/opt/zk/zookeeper-3.4.6/bin/zkServer.sh restart
#/opt/zk/zookeeper-3.4.6/bin/zkServer.sh status
#echo 1 >/opt/zookeeper/zookeeper/data
# sever-1:0.0.0.0:2888:3888




